﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace HauhaltsPlaner
{
    public partial class Profile : Form
    {
        DataTable dt;

        public Profile()
        {
            InitializeComponent();
        }

        private void Profile_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            InitListbox1();
            label1.Text = "-/-";
        
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IOHandler writer = new IOHandler();
          
            

            if (listBox1.Items.Contains(textBox1.Text))
            {
                label1.Text = "Dieses Profil exestiert leider schon. \n" + "Bitte wähle einen anderen Namen.";
            }
            else
            {
                dt.Rows.Add(textBox1.Text);

                writer.saveDataTable("", "", dt);
                InitListbox1();
                label1.Text = "Daten erfolgreich übertragen.";
            }
        }

        private void InitListbox1()
        {
            if (System.IO.File.Exists("./ProfilData.xml"))
            {
                IOHandler reader = new IOHandler();

                listBox1.Items.Clear();

                dt = reader.loadDataTable();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listBox1.Items.Add(dt.Rows[i][0]);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                IOHandler del = new IOHandler();

                for (int i = dt.Rows.Count - 1; i > -1; i--)
                {
                    if (listBox1.SelectedIndex == i)
                    {
                        del.deleteProfile((string)dt.Rows[i][0]);
                        dt.Rows[i].Delete();
                    }
                }

                IOHandler writer = new IOHandler();
                writer.saveDataTable("", "", dt);
                InitListbox1();
               
                label1.Text = "Profil erfolgreich entfernt.";
            }
            else
            {
                label1.Text = "Kein Profil markiert \n" + "Bitte wähle ein Profil aus.";
            }
        }
    }
}
